package com.developer.platform.application.query.assembler;

import cn.hutool.core.collection.CollUtil;
import com.developer.platform.application.query.dto.BaseDto;
import com.developer.platform.domain.Group;
import com.developer.platform.domain.Systems;
import com.developer.platform.domain.Template;
import com.developer.platform.domain.User;
import com.developer.platform.infrastructure.data.mapper.GroupMapper;
import com.developer.platform.infrastructure.data.mapper.SystemMapper;
import com.developer.platform.infrastructure.data.mapper.TemplateMapper;
import com.developer.platform.infrastructure.data.mapper.UserMapper;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author ：qiaoliang
 * @date ：2020-11-12
 */
public class BaseAssembler {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private GroupMapper groupMapper;
    @Autowired
    private SystemMapper systemMapper;
    @Autowired
    private TemplateMapper templateMapper;

    public <T extends BaseDto> void userName(T baseDto){
        List<Long> userIdList = Lists.newLinkedList();
        if(!userIdList.contains(baseDto.getCreateBy())){
            userIdList.add(baseDto.getCreateBy());
        }
        if(!userIdList.contains(baseDto.getUpdateBy())){
            userIdList.add(baseDto.getUpdateBy());
        }
        Map<Long, String> longStringMap = this.userNameMap(userIdList);
        baseDto.setCreateByName(longStringMap.get(baseDto.getCreateBy()));
        baseDto.setUpdateByName(longStringMap.get(baseDto.getUpdateBy()));
    }

    public <T extends BaseDto> void userName(List<T> list){
        if(CollUtil.isNotEmpty(list)){
            List<Long> userIdList = Lists.newLinkedList();
            for (BaseDto baseDto : list) {
                if(!userIdList.contains(baseDto.getCreateBy())){
                    userIdList.add(baseDto.getCreateBy());
                }
                if(!userIdList.contains(baseDto.getUpdateBy())){
                    userIdList.add(baseDto.getUpdateBy());
                }
            }
            Map<Long, String> longStringMap = this.userNameMap(userIdList);
            for (BaseDto record : list) {
                record.setCreateByName(longStringMap.get(record.getCreateBy()));
                record.setUpdateByName(longStringMap.get(record.getUpdateBy()));
            }
        }
    }


    /**
     *  通过用户ID查询用户名称
     *
     * @author ：qiaoliang
     * @param ids : 用户id
     * @return java.util.Map<java.lang.String, java.lang.String>
     * @date 2020-11-12 14:12
     */
    public Map<Long, String> userNameMap(List<Long> ids){
        if(CollUtil.isEmpty(ids)){
            return Maps.newHashMap();
        }
        return userMapper.selectBatchIds(ids).stream()
            .collect(Collectors.toMap(User::getId, User::getName));
    }

    /**
     *  通过用户ID查询用户手机号
     *
     * @author ：qiaoliang
     * @param ids : 用户id
     * @return java.util.Map<java.lang.String, java.lang.String>
     * @date 2020-11-12 14:12
     */
    public Map<Long, String> userPhoneMap(List<Long> ids){
        if(CollUtil.isEmpty(ids)){
            return Maps.newHashMap();
        }
        return userMapper.selectBatchIds(ids).stream()
            .collect(Collectors.toMap(User::getId, User::getPhone));
    }

    /**
     *  通过群ID查询群名称
     *
     * @author ：qiaoliang
     * @param ids : 群id
     * @return java.util.Map<java.lang.String, java.lang.String>
     * @date 2020-11-12 14:12
     */
    public Map<Long, String> groupNameMap(List<Long> ids){
        if(CollUtil.isEmpty(ids)){
            return Maps.newHashMap();
        }
        return groupMapper.selectBatchIds(ids).stream()
            .collect(Collectors.toMap(Group::getId, Group::getName));
    }

    /**
     *  通过系统ID查询系统名称
     *
     * @author ：qiaoliang
     * @param ids : 系统id
     * @return java.util.Map<java.lang.String, java.lang.String>
     * @date 2020-11-12 14:12
     */
    public Map<Long, String> systemNameMap(List<Long> ids){
        if(CollUtil.isEmpty(ids)){
            return Maps.newHashMap();
        }
        return systemMapper.selectBatchIds(ids).stream()
            .collect(Collectors.toMap(Systems::getId, Systems::getName));
    }

    /**
     *  通过模板ID查询模板名称
     *
     * @author ：qiaoliang
     * @param ids : 模板ID
     * @return java.util.Map<java.lang.String, java.lang.String>
     * @date 2020-11-12 14:12
     */
    public Map<Long, String> templateNameMap(List<Long> ids){
        if(CollUtil.isEmpty(ids)){
            return Maps.newHashMap();
        }
        return templateMapper.selectBatchIds(ids).stream()
            .collect(Collectors.toMap(Template::getId, Template::getName));
    }

}
